//sc_shop_app
(function (angular) {
  'use strict';

  var module = angular.module('sc_shop_app');

  module.factory('noticeService', function ($http, $rootScope) {

    var service = {};
    service.test = function () {
      //service 测试方法
      alertify.alert('', '测试成功');
    };

    return service;

  });

  module.factory('addressService', function ($http, $rootScope) {
    //添加收货地址 并设置为默认
    var service = {};

    service.queryString = function (val) {
      var uri = window.location.search;
      var re = new RegExp("" + val + "=([^&?]*)", "ig");
      return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);

    }

    service.addAddress = function (data, callback) {

      $http({
        url: API.ADD_ADDRESS + "?_t=" + (new Date().getTime()),
        method: 'POST',
        data: data,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
        }
      }).success(function (data) {
        callback(data);
      }).error(function () {
        //网络连接错误
        mui.alert("请检查您的网络");
      });
    }
    service.getAddressList = function (param, callback) {
      $http({
        method: 'GET',
        url: API.ADDRESS_LIST + param.accountId
      }).success(function (data) {
        callback(data);
      }).error(function () {
        //网络连接错误
        mui.alert("请检查您的网络");
      });
    }
    service.deleteAddress = function (id, callback) {
      $http({
        url: API.DELETEADDRESS + id,
        method: 'GET',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
        }
      }).success(function (data) {
        callback(data);
      }).error(function () {
        //网络连接错误
        mui.alert("请检查您的网络");
      });
    }

    return service;
  });

  module.factory("myNoticeService", function ($http, $rootScope) {
    var service = {};
    service.test = function (param) {
      alertify.alert('', '这是一个测试' + param);
    }
    service.queryString = function (val) {
      var uri = window.location.search;
      var re = new RegExp("" + val + "=([^&?]*)", "ig");
      return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);

    }
    service.getNoticeDetail = function (notice_id, notice_status, callback) {

      $http({
        url: API.APPDETAIL + notice_id + "?_t=" + (new Date().getTime()),
        method: 'GET'
      }).success(function (data) {
        callback(data);
      }).error(function () {
        alertify.alert('温馨提示', '网络错误');
      });
    }
    return service;
  });
  module.factory("myOrderService", function ($http, $rootScope) {
    //订单service
    var service = {};
    return service;
  });

  module.factory("orderDetailService", function ($http, $rootScope) {
    var service = {};
    service.queryString = function (val) {
      var uri = window.location.search;
      var re = new RegExp("" + val + "=([^&?]*)", "ig");
      return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);

    }

    //查询订单详情
    service.getOrderDetail = function (id, callback) {
      return $http({
        url: API.ORDERDETAIL + id + "?_t=" + (new Date().getTime()),
        method: 'GET'
      }).success(function (data) {
        callback(data);
      }).error(function (data) {
        mui.alert("网络连接错误", "温馨提示");
      });
    }

    return service;
  });

  module.factory("regService", function ($http, $rootScope) {

    var service = {};
    service.register = function (accountInfo, callback) {
      //注册账号
      $http({
        url: API.REGISTER,
        method: 'POST',
        data: accountInfo,
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
        }
      }).success(function (data) {
        callback(data);
      }).error(function () {
        mui.alert("请检查您的网络", "提示");
      });
    }
    return service;
  });

  module.factory("suggestionService", function ($http, $rootScope) {
    var service = {};
    service.getSuggestionList = function (callback) {
      $http({
        url: API.SUGGESTIONLIST + "?_t=" + (new Date().getTime()),
        headers: {
          "X-Security-Token": getCache("token"),
        },
        method: "GET",
        cache: false,
        timeout: 5000
      }).success(function (data) {
        callback(data);
      }).error(function () {
        mui.alert("请检查您的网络", "提示");
      });
    }

    service.commitSuggestion = function (content, callback) {
      $http({
        method: "POST",
        url: API.SUGGESTIONCOMMIT + "?_t=" + (new Date().getTime()),
        data: {
          content: content,
        },
        headers: {
          "X-Security-Token": getCache("token"),
        },
        contentType: "application/x-www-form-urlencoded; charset=utf-8"
      }).success(function (data) {
        callback(data);
      }).error(function () {
        mui.alert("请检查您的网络", "提示");
      });
    }

    service.getSuggestionDetail = function (id, callback) {
      $http({
        url: API.SUGGESTIONDETAIL + id + "?_t=" + (new Date().getTime()),
        method: "GET",
        cache: false,
         headers: {
          "X-Security-Token": getCache("token"),
        },
      }).success(function (data) {
        callback(data);
      }).error(function () {
        mui.alert("请检查您的网络", "提示");
      });
    }
    return service;

  });


})(window.angular);